﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Close.Controls
{
    public class NotifyList : BindingList<object>
    {
        private bool modified;

        public bool Modified
        {
            get { return modified; }
            set
            {
                modified = value;
                if (modified) OnModified();
            }
        }

        private void OnModified()
        {
            if (ModifyChanged != null) ModifyChanged(this, EventArgs.Empty);
        }

        public event EventHandler ModifyChanged;

        protected override void OnListChanged(ListChangedEventArgs e)
        {
            base.OnListChanged(e);

            switch (e.ListChangedType)
            {
                case ListChangedType.ItemAdded:
                    PrepareItem(this[e.NewIndex]);
                    break;
            }
            Modified = true;
        }

        private void PrepareItem(object item)
        {
            NotifyItemBase nib = item as NotifyItemBase;
            if (nib != null) nib.List = this;
        }


    }
    class NotifyItemBase
    {
        public NotifyList List { get; set; }

        public void Modify()
        {
            if (List != null) List.Modified = true;
        }
    }

    class LevelListBoxItem : NotifyItemBase
    {
        public LevelCategory Category { get; set; }

        public string Title { get { return Category.Title; } }

        public int Lid { get { return Category.Id; } }

        /// <summary>
        /// Gets wether the item is already available.
        /// </summary>
        //public bool IsUsed { get; set; }
    }
}
